/**
 * OptionsDialog.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import webcrawler.activities.data.Data;
import webcrawler.activities.data.DataFile;
import webcrawler.activities.data.DataManager;
import webcrawler.activities.view.utils.Browsers;
import webcrawler.activities.view.utils.ExtendedComboBox;

/**
 * The Class OptionsDialog.
 * 
 * @author Gerson Groth
 */
@SuppressWarnings("serial")
public class OptionsDialog extends JDialog {

	/** The content panel. */
	private final JPanel contentPanel = new JPanel();

	/** The browser combo box. */
	private JComboBox<Browsers> browserComboBox;

	/**
	 * Create the dialog.
	 */
	public OptionsDialog() {

		setDefaultOptions();

		createBrowsersPanel();

		createSaveButton();
	}

	/**
	 * Creates the save button.
	 */
	private void createSaveButton() {
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				save(arg0);
			}

		});
		btnSave.setBounds(91, 82, 89, 23);
		contentPanel.add(btnSave);

	}

	/**
	 * Save.
	 *
	 * @param e the e
	 */
	private void save(ActionEvent e) {
		DataFile.getInstance().setBrowser(
				(Browsers) browserComboBox.getSelectedItem());
		DataManager.getINSTANCE().saveData();
		dispose();
	}

	/**
	 * Creates the browsers panel.
	 */
	@SuppressWarnings("unchecked")
	private void createBrowsersPanel() {
		JPanel browsersPanel = new JPanel();
		browsersPanel.setBorder(new TitledBorder(null, "Choose your browser",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		browsersPanel.setBounds(10, 11, 266, 60);
		contentPanel.add(browsersPanel);
		browsersPanel.setLayout(null);

		browserComboBox = new ExtendedComboBox();
		browserComboBox.setBounds(66, 23, 120, 20);
		browsersPanel.add(browserComboBox);

		JLabel lblBrowser = new JLabel("Browser:");
		lblBrowser.setBounds(10, 26, 46, 14);
		browsersPanel.add(lblBrowser);
		ExtendedComboBox ex = (ExtendedComboBox) browserComboBox;

		for (Browsers b : Browsers.values()) {
			ex.addItem(b);
		}
		ex.setSelectedItem(Data.getInstance().getBrowser());

	}

	/**
	 * Sets the default options.
	 */
	private void setDefaultOptions() {
		setModal(true);
		setTitle("Options");
		setBounds(100, 100, 300, 158);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

	}
}
